import rclpy
from rclpy.node import Node
from example_interfaces.msg import String
from queue import Queue
import espeakng
import threading
import time

class NovelSubNode(Node):

    def __init__(self,node_name):
        super().__init__(node_name)
        self.get_logger().info(f'{node_name} start !')
        self.novel_queue=Queue()
        self.novel_sub =self.create_subscription(String,"novel_pub",self.novel_sub_callback,10)
        self.speak_thread=threading.Thread(target=self.speaker_thread)
        self.speak_thread.start()
        

    def novel_sub_callback(self,msg):
        self.novel_queue.put(msg.data)

    def speaker_thread(self):
        speaker=espeakng.Speaker()
        speaker.voice ='zh'

        while rclpy.ok():
            if self.novel_queue.qsize()>0 :
                text=self.novel_queue.get()
                self.get_logger().info("text")
                speaker.say(text)
                speaker.wait()
            else:
                time.sleep(1)


def main():
    rclpy.init()
    node = NovelSubNode("novel_sub")
    
    rclpy.spin(node)
    rclpy.shutdown()